package com.arm.controller.admin.product;

import com.arm.controller.BaseController;
import com.arm.def.Constant;
import com.arm.module.ProductEntity;
import com.arm.service.ProductEntityService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RequestMapping("/admin/productEntity")
@RestController
public class AdminProductEntityController extends BaseController {

    @Autowired
    ProductEntityService productEntityService;

    /**
     * 拉取所有产品实体（没有使用查询图片方法）
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProductEntityList")
    public ModelAndView getProductEntityList(Long productId, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("admin/product/getProductEntityList");
        Map<String, String> map = this.showRequestParameters(super.httpServletRequest);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<ProductEntity> productEntityList = productEntityService.getProductEntityList(map);
        PageInfo<ProductEntity> pageinfo = new PageInfo<>(productEntityList);
        m.addObject("pageinfo", pageinfo);
        m.addObject("bean", map);
        m.addObject("productId", productId);
        return m;
    }

    /**
     * 跳转到添加或修改页面
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProductEntity")
    public ModelAndView getProductEntity(HttpServletRequest request) {
        ModelAndView m = new ModelAndView("admin/product/getProductEntity");
        Map<String, String> map = this.showRequestParameters(request);
        String productEntityId = map.get("productEntityId");
        String productId = map.get("productId");
        ProductEntity productEntity = new ProductEntity();
        if (StringUtils.isNotBlank(productEntityId)) {
            productEntity = productEntityService.getProductEntityByProductEntityId(Long.parseLong(productEntityId));
            productId = String.valueOf(productEntity.getProductId());
        }
        m.addObject("productEntity", productEntity);
        m.addObject("productId", productId);
        return m;
    }

    /**
     * 添加或修改产品实体
     *
     * @return
     */
    @PostMapping(value = "/modifiyProductEntity")
    public String modifiyProductEntity(ProductEntity productEntity, @RequestParam(value = "productEntityPicUrl", required = false) MultipartFile productEntityPicUrl, @RequestParam(value = "productEntityInfoPicUrl", required = false) MultipartFile productEntityInfoPicUrl) {
        ResultObj resultObj = new ResultObj();
        String pic = "";
        String filePath = "autumnrainmorning2/product";
        logger.info("=====================>>>" + filePath);
        try {
            if (productEntity.getProductId() == null) {
                throw new MyRunTimeExcption("产品不能为空！");
            }

            if (productEntityPicUrl != null) {
                try {

                    pic = upLoadFile(productEntityPicUrl.getInputStream(), filePath);
                    logger.info("resourceUrl=============>>>" + pic);
                    productEntity.setProductEntityPic(pic);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (productEntityInfoPicUrl != null) {
                try {

                    pic = upLoadFile(productEntityInfoPicUrl.getInputStream(), filePath);
                    logger.info("resourceUrl=============>>>" + pic);
                    productEntity.setPicUrl(pic);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            productEntity = productEntityService.modifyProductEntity(productEntity);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
}
